package shc.graduation.http.getter;

import android.text.TextUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import shc.graduation.bean.PaperInfo;

/**
 * Created by shuhuiChen on  2016/5/21  18:21.
 * Email ：shuhuiCSH@126.com
 */
public class PaperInfoGetter extends BaseGetter<PaperInfo> {
    @Override
    public List<PaperInfo> getModel(String websiteUrl) {

        String webContent = mIHttpGet.getContent(websiteUrl);

        if (TextUtils.isEmpty(webContent)) {
            return null;
        }

        mListModel = new ArrayList<>();

        Document doc = Jsoup.parse(webContent);

        Elements trs = doc.select("div[class=report]");
        parseElements(trs);

        trs = doc.select("div[class=weibo]");
        parseElements(trs);

        return mListModel;
    }

    @Override
    public String getEncode() {
        return "utf-8";
    }

    private void parseElements(Elements trs) {
        PaperInfo paperInfo;
        Elements elements = null;
        boolean isCanSave;

        for (Element tr : trs) {

            paperInfo = new PaperInfo();

            isCanSave = false;

            elements = tr.select("img[class=profile_image]");
            if (elements != null && elements.size() > 0) {
                String authorAvatar = elements.get(0).attr("src") + "";
                paperInfo.setAuthorAvatar(authorAvatar);
            }

            elements = tr.select("a[class=link_screen_name]");
            if (elements != null && elements.size() > 0) {
                String authorAddress = elements.get(0).attr("href") + "";
                paperInfo.setAuthorAddress(authorAddress);

                elements = elements.get(0).select("b[class=screen_name]");
                if (elements != null && elements.size() > 0) {
                    String author = elements.get(0).text() + "";
                    paperInfo.setAuthor(author);
                }
            }

            elements = tr.select("a[class=link_weibo_source]");
            if (elements != null && elements.size() > 0) {
                String fromAddress = elements.get(0).attr("href") + "";
                paperInfo.setFromAddress(fromAddress);

                isCanSave = true;
            }

            elements = tr.select("span[class=datetime]");
            if (elements != null && elements.size() > 0) {
                String paperTime = elements.get(0).text() + "";
                paperInfo.setPaperTime(paperTime);
            }

            elements = tr.select("div[class=keyword-wrapper]");
            if (elements != null && elements.size() > 0) {
                elements = elements.get(0).getElementsByTag("span");
                if (elements != null && elements.size() > 0) {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0, size = elements.size(); i < size; i++) {
                        stringBuilder.append(elements.get(i).text());
                        if (i < (size - 1)) {
                            stringBuilder.append(",");
                        }
                    }
                    if (!TextUtils.isEmpty(stringBuilder.toString())) {
                        paperInfo.setTagList(stringBuilder.toString());
                    }
                }
            }

            elements = tr.select("div[class=text]");
            if (elements != null && elements.size() > 0) {

                //替换链接里面的text
                Elements elements1 = elements.get(0).getElementsByTag("a");
                for (Element element : elements1) {
                    if (!TextUtils.isEmpty(element.attr("title"))) {
                        element.text(element.attr("title"));
                    }
                }

                String content = elements.get(0).text() + "";
                paperInfo.setContent(content);
            }

            elements = tr.select("div[class=original_pic]");
            if (elements != null && elements.size() > 0) {
                elements = elements.get(0).getElementsByTag("a");
                if (elements != null && elements.size() > 0) {
                    String photoUrl = elements.get(0).attr("href") + "";
                    paperInfo.setPhotoUrl(photoUrl);
                }
            }

            if (isCanSave) {
                mListModel.add(paperInfo);
            }
        }
        if (trs != null && trs.size() > 0) {
            trs.clear();
        }
    }
}
